<?php

/**
 * This is the model class for table "goods_agent_related".
 *
 * The followings are the available columns in table 'goods_agent_related':
 * @property integer $id ID
 * @property string $provider_id Provider
 * @property string $dealer_id Dealer
 * @property string $goods_id Goods
 */
class GoodsAgentRelated extends ActiveRecord
{
	public $goods_name;
	/**
	 * Returns the static model of the specified AR class.
	 * @return GoodsAgentRelated the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	
	public static function agent($provider_id,$goods_id,$dealer_id) {
		if(self::isAgented($provider_id, $goods_id, $dealer_id)) return true;
		$model = new self();
		$model->attributes = array(
			'provider_id' => $provider_id,
			'goods_id' => $goods_id,
			'dealer_id' => $dealer_id,
		);
		return $model->save();
	}
	
	public static function deagent($provider_id,$goods_id,$dealer_id) {
		$model = self::model()->findByAttributes(array(
			'provider_id' => $provider_id,
			'goods_id' => $goods_id,
			'dealer_id' => $dealer_id,
		));
		return $model ? $model->delete() : false;
	}
	
	public static function isAgented($provider_id,$goods_id,$dealer_id) {
		return self::model()->countByAttributes(array(
			'provider_id' => $provider_id,
			'goods_id' => $goods_id,
			'dealer_id' => $dealer_id,
		));
	}
}